<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <script src="../js/vue.min.js"></script>
    <script src="../js/vue-router.js"></script>
    <title>编程式导航</title>
  </head>
  <body>
    <div id="app">
      <!-- <router-link to="/a">首页</router-link> -->
      <button type="button" @click="test">按钮</button>
      <button type="button" @click="test2">测试编程式导航传值</button>
      <button type="button" @click="test3">测试编程式导航传值3</button>
      <!-- 命名路由 -->
      <router-link :to="{name:'r1'}">测试命名路由link</router-link>
      <button type="button" @click="test4">测试命名路由click</button>
      <router-view></router-view>
    </div>
    <script>
      const t1 = {
        template: `<div style="width:400px;height:200px;border:blue 1px solid">index{{$route.params.id}}</div>`,
      };
      const t2 = {
        template: `<div style="width:400px;height:200px;border:blue 1px solid">命名路由</div>`,
      };
      const myRouter = new VueRouter({
        routes: [
          {
            path: "/a/:id",
            component: t1,
          },
          {
            path: "/c",
            name: "r1",
            component: t2,
          },
          { path: "/b", component: t1 },
        ],
      });
      var vue = new Vue({
        el: "#app",
        router: myRouter,
        methods: {
          test: function () {
            // js代码实现路由跳转，编程式导航
            myRouter.push("/a/101");
          },
          // push对象参数
          test2: function () {
            // js代码实现路由跳转，编程式导航
            myRouter.push({ name: "r1", params: { id: 102 } });
          },
          test3: function () {
            // 第三种方式 query url传值
            myRouter.push({ path: "/b", query: { id: 103 } });
          },
          test4() {
            myRouter.push({name:'r1'})
          },
        },
      });
    </script>
  </body>
</html>
